import { reactive, onMounted } from 'vue'
import {
  getSysUserListReq,
  resetPwdReq,
  updateUserStateReq
} from '@/service/modules/user'
import { format } from 'date-fns'
import { parseTime } from '@/common/common'
import { createUserDbReq } from '@/service/modules/data-manage'
import { UserInfo } from '@/service/modules/user/types'

export function useTable() {
  const state = reactive({
    page: 1,
    limit: 10,
    count: 0,
    searchParam: {
      username: '',
      nickname: '',
      departmentId: null
    },
    list: [],
    loading: false,
    currentRecord: {} as UserInfo,
    userDetailModalShow: false,
    modRolesModalShow: false,
    modDepartmemtModalShow: false,
    createUserModalShow: false,
    setHiveDbSizeModalShow: false
  })

  const getList = async () => {
    if (state.loading) return
    state.loading = true

    const { data, count } = await getSysUserListReq({
      page: state.page,
      limit: state.limit,
      ...state.searchParam
    })
    state.loading = false
    if (!data) throw Error()
    state.list = data.map((record: UserInfo) => {
      record.createTime = record.createTime
        ? format(parseTime(record.createTime), 'yyyy-MM-dd HH:mm:ss')
        : ''
      record.updateTime = record.updateTime
        ? format(parseTime(record.updateTime), 'yyyy-MM-dd HH:mm:ss')
        : ''
      return record
    })
    state.count = count
  }

  const updateList = () => {
    if (state.list.length === 1 && state.page > 1) {
      --state.page
    }
    getList()
  }

  const updateUserState = async (userId: number, enabled: boolean) => {
    await updateUserStateReq({ userId: userId, enabled: enabled })
    updateList()
  }

  const createUserDb = async (username: string) => {
    await createUserDbReq({ username: username })
  }

  const resetPwd = async (userId: number) => {
    await resetPwdReq(userId)
  }

  const onOperationClick = (
    data: { rowData: UserInfo },
    type:
      | 'modRoles'
      | 'modDepartment'
      | 'updateUserState'
      | 'showUserDetail'
      | 'createUserDb'
      | 'setUserDbSize'
      | 'resetPwd',
    otherParams?: any
  ) => {
    state.currentRecord = data.rowData
    switch (type) {
      case 'modRoles':
        state.modRolesModalShow = true
        break
      case 'modDepartment':
        state.modDepartmemtModalShow = true
        break
      case 'updateUserState':
        updateUserState(data.rowData.id, otherParams)
        break
      case 'showUserDetail':
        state.userDetailModalShow = true
        break
      case 'createUserDb':
        createUserDb(data.rowData.username)
        break
      case 'setUserDbSize':
        state.setHiveDbSizeModalShow = true
        break
      case 'resetPwd':
        resetPwd(data.rowData.id)
        break
    }
  }

  const changePage = (page: number) => {
    state.page = page
    getList()
  }

  const changePageSize = (limit: number) => {
    state.page = 1
    state.limit = limit
    getList()
  }

  onMounted(() => {
    getList()
  })

  return {
    state,
    changePage,
    changePageSize,
    updateList,
    updateUserState,
    onOperationClick
  }
}
